Fix memory leak when creating MPIs for ECGroup - #5381
Merged
Conversation
arckoor
force-pushed
the
python-ec-group-memleak
branch
from
February 22, 2026 15:16
d45efc6 to
a92e237
Compare
randombit
approved these changes
Feb 22, 2026
There was a problem hiding this comment.
Pull request overview
This pull request fixes memory leaks in the ECGroup class methods that retrieve MPI (multi-precision integer) values. The issue was that calling MPI() initializes an MPI object by allocating memory via botan_mp_init, but then the FFI functions like botan_ec_group_get_p would overwrite the pointer with a newly allocated MPI, causing the first allocation to leak.
Changes:
- Modified MPI.init to accept c_void_p and skip initialization when a raw pointer is provided
- Updated all six ECGroup getter methods (get_p, get_a, get_b, get_g_x, get_g_y, get_order) to use the new pattern
- Updated version comment from 3.10.0 to 3.11.0
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In #5166 (comment) copilot called out that what I was doing in the python code would leak the MPI initialized by
__init__. This is the same thingECGroupis already doing, so fix it.To be honest, I don't love this fix, I've omitted the
| c_void_pfrom theMPILiketype hint, because it's used in other places and I don't think encouraging people to pass a magic pointer that hopefully is an MPI to something likePrivateKey.load_ecdsa(curve: str, x: MPILike)is a good idea.The
MPIclass is very unfortunate, all the other classes basically just assign a pointer toself.__objand let whatever else do the proper initialization,MPIis seemingly the only one that does extra stuff in its constructor.I considered just skipping
__init__and implementing / calling__new__directly, but it appears that is frowned upon in the python community (which is probably a good thing).